import {useCounterStore} from "@/store/counter";
import type {App} from "vue";

const hasRole = {
  mounted(el: any, binding: any, VNode: any) {
    const store = useCounterStore() as any;
    const {value} = binding
    const super_admin = "admin";
    const roles = store.roles
    if (value && value instanceof Array && value.length > 0) {
      const roleFlag = value

      const hasRole = roles.some((role: string) => {
        return super_admin === role || roleFlag.includes(role)
      })

      if (!hasRole) {
        VNode.ctx.props.disabled = true //指令默认开启element plus的el-button disabled属性
        // el.parentNode && el.parentNode.removeChild(el) //如果想移除当前button请解注这段代码
      }
    } else {
      throw new Error(`请设置角色权限标签值"`)
    }
  }
}

export default {
  install(app: App<Element>) {
    app.directive('hasRole', hasRole)
  }
}
